package com.lam.dormitory.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lam.dormitory.entity.Admin;
import com.lam.dormitory.entity.Dormitory;
import com.lam.dormitory.service.IAdminService;
import com.lam.dormitory.service.IDormitoryService;
import com.lam.dormitory.service.IDormitoryServiceService;
import com.lam.dormitory.util.ResponseTemplate;
import com.lam.dormitory.vo.DormitoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lam
 * @since 2019-12-26
 */
@Api(tags = "宿舍信息接口")
@RestController
@RequestMapping("/dormitory")
public class DormitoryController {
    @Autowired
    private IDormitoryService dormitoryService;
    @Autowired
    private IAdminService adminService;

    /**
     * 添加宿舍
     *
     * @param dormitory
     * @return
     */
    @ApiOperation(value = "添加宿舍", notes = "添加宿舍")
    @PostMapping("/add")
    @RequiresRoles(value = {"admin", "superadmin"}, logical = Logical.OR)
    public ResponseTemplate addDormitory(Dormitory dormitory) {
        QueryWrapper<Dormitory> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Dormitory::getBuildingNo, dormitory.getBuildingNo()).eq(Dormitory::getDorNo, dormitory.getDorNo());
        Dormitory one = dormitoryService.getOne(wrapper);
        if (one != null) {
            return new ResponseTemplate(500, "宿舍已存在");
        }
        dormitory.setStatus(1);
        boolean save = dormitoryService.save(dormitory);
        if (save) {
            return new ResponseTemplate(200, "添加成功");
        }
        return new ResponseTemplate(500, "添加失败");
    }


    /**
     * 删除宿舍
     *
     * @param dorId
     * @return
     */
    @ApiOperation(value = "删除宿舍", notes = "根据dorId删除宿舍")
    @PostMapping("/delete")
    @RequiresRoles(value = {"admin", "superadmin"}, logical = Logical.OR)
    public ResponseTemplate deleteDormitory(Long dorId) {
        QueryWrapper<Dormitory> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Dormitory::getDorId, dorId);
        boolean remove = dormitoryService.remove(wrapper);
        if (remove) {
            return new ResponseTemplate(200, "删除成功");
        }
        return new ResponseTemplate(500, "删除失败");
    }

    /**
     * 修改宿舍信息
     *
     * @param dormitory
     * @return
     */
    @ApiOperation(value = "修改宿舍信息", notes = "根据dorId修改宿舍信息")
    @PostMapping("/edit")
    @RequiresRoles(value = {"admin", "superadmin"}, logical = Logical.OR)
    public ResponseTemplate editDormitory(Dormitory dormitory) {
        QueryWrapper<Dormitory> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Dormitory::getDorId, dormitory.getDorId());
        boolean update = dormitoryService.update(dormitory, wrapper);
        if (update) {
            return new ResponseTemplate(200, "修改成功");
        }
        return new ResponseTemplate(500, "修改失败");
    }

    /**
     * 可根据dorId,buildingNo和dorNo查询宿舍信息
     *
     * @param dormitory
     * @return
     */
    @ApiOperation(value = "查询宿舍信息", notes = "可根据dorId,buildingNo和dorNo查询宿舍信息，均为空, type =1时搜索dormitory表无重复")
    @PostMapping("/list")
    @RequiresRoles(value = {"admin", "superadmin","student"}, logical = Logical.OR)
    public ResponseTemplate selectDormitory(Dormitory dormitory, Integer type, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "0") Integer pageSize) {
        if (type != null) {
            PageHelper.startPage(pageNum, pageSize);
            List<Dormitory> dormitoryList = dormitoryService.selectBuildingNo();
            PageInfo<Dormitory> pageInfo = new PageInfo<>(dormitoryList);
            return new ResponseTemplate(200, "查询成功", dormitoryList, pageInfo.getTotal());
        }else {
            PageHelper.startPage(pageNum, pageSize);
            List<DormitoryVo> dormitoryList = dormitoryService.selectDorByDormitory(dormitory);
            PageInfo<DormitoryVo> pageInfo = new PageInfo<>(dormitoryList);
            return new ResponseTemplate(200, "查询成功", dormitoryList, pageInfo.getTotal());
        }
    }
    @ApiOperation(value = "宿舍分配学生", notes = "dorId,userIds")
    @PostMapping("/assign/student")
    @RequiresRoles(value = {"admin", "superadmin"}, logical = Logical.OR)
    public ResponseTemplate addAssignStudents(Long dorId, @RequestParam(value = "userIds")List<Long> userIds){
        ResponseTemplate responseTemplate = dormitoryService.addAssignStudents(dorId, userIds);
        return responseTemplate;
    }

    @ApiOperation(value = "宿舍分配管理员", notes = "根据dorId,userIds")
    @PostMapping("/assign/admin")
    @RequiresRoles(value = {"admin", "superadmin"}, logical = Logical.OR)
    public ResponseTemplate addAssignAdmins(Long dorId, @RequestParam(value = "userIds")List<Long> userIds){
        Admin admin = new Admin();
        admin.setDorId(dorId);
        for (Long userId : userIds) {
            QueryWrapper<Admin> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(Admin::getUserId,userId);
            adminService.update(admin,wrapper);
            return new ResponseTemplate(200,"分配成功");
        }
        return new ResponseTemplate(500, "分配失败");
    }

}
